<template>
  <el-dialog
    v-el-drag-dialog
    title="退款申请"
    :visible.sync="visible"
    width="700px"
    @closed="reset"
  >
    <el-form ref="dataForm" class="form" :rules="rules" :model="temp" label-position="right" label-width="80px">
      <el-form-item label="客户名称" prop="csmName">
        <el-input v-model="temp.csmName" disabled />
      </el-form-item>
      <el-form-item label="客户电话" prop="csmPhone">
        <el-input v-model="temp.csmPhone" disabled />
      </el-form-item>
      <el-form-item class="block" label="客户地址" prop="csmAddress">
        <el-input v-model="temp.csmAddress" disabled />
      </el-form-item>
      <el-form-item label="支付方式" prop="payType">
        <el-input :value="formatColumnMap(payTypeMap, 'value', temp.payType, 'desc')" disabled />
      </el-form-item>
      <el-form-item label="订单金额" prop="billTotalFee">
        <el-input v-model="temp.billTotalFee" disabled />
      </el-form-item>
      <el-form-item label="退款渠道" prop="refundchannel">
        <el-select v-model="temp.refundchannel" placeholder="请选择">
          <el-option
            v-for="item in refundchannelMap"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="退款金额" prop="refundFee">
        <el-input v-model="temp.refundFee" disabled />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.submit" type="primary" @click="success">
        确认
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { queryMap, queryRefundByWFT, queryRefundByWechat, queryRefundByZFB, queryRefundByICBCWallet, queryRefundByICBCQrcode } from '@/api'
  import { formatColumnMap } from '@/utils'

  export default {
    name: 'detail',
    data() {
      return {
        formatColumnMap,
        visible: false,
        payTypeMap: [],
        refundchannelMap: [],
        temp: {
          refundchannel: ''
        },
        rules: {
          refundchannel: [{ required: true, message: '请选择退款渠道', trigger: 'change' }]
        }
      }
    },
    methods: {
      async open(temp, payTypeMap) {
        this.payTypeMap = payTypeMap
        await this.queryOptionsMap()
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.temp = Object.assign(this.temp, temp)
          }
        })
      },
      // 查询枚举
      async queryOptionsMap() {
        const res = await queryMap({ enumName: 'BusRefundChannel' })
        this.refundchannelMap = res.data // 退款渠道
        this.temp.refundchannel = 2
      },
      success() {
        this.$refs.dataForm.validate(async valid => {
          if (!valid) return
          const params = {
            refundBillCode: this.temp.refundBillCode,
            refund_account: this.temp.refundchannel === 1 ? 'REFUND_SOURCE_RECHARGE_FUNDS' : 'REFUND_SOURCE_UNSETTLED_FUNDS',
            refundfee: this.temp.refundFee
          }
          this.submit(params)
        })
      },
      async submit(params) {
        switch (this.temp.payType) {
        case 1: // 微信支付
          await queryRefundByWechat(params)
          break
        case 8: // 威富通支付
          await queryRefundByWFT(params)
          break
        case 2: // 支付宝支付
          await queryRefundByZFB(params)
          break
        case 32: // 工行钱包
          await queryRefundByICBCWallet(params)
          break
        case 64: // 工行扫码支付
          await queryRefundByICBCQrcode(params)
          break
        }
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '转入成功',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.temp = {
          refundchannel: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.form {
  display: flex;
  flex-wrap: wrap;
  & /deep/ .el-form-item {
    width: 50%;
    padding-right: 10px;
    .el-form-item__content > .el-select {
      width: 100%;
    }
  }
  .block {
    width: 100%;
  }
}
.info-title {
  display: flex;
  align-items: center;
  margin-top: 10px;
  margin-bottom: 20px;
  .title-icon {
    display: inline-block;
    width: 4px;
    height: 18px;
    margin-right: 6px;
    background: $themeColor;
  }
  span {
    font-weight: 700;
    color: $themeColor;
  }
}
</style>
